package com.qlk.vorder.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qlk.common.utils.page.PageUtil;
import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;
import com.qlk.vorder.controller.base.BaseController;
import com.qlk.vorder.domain.Task;
import com.qlk.vorder.domain.enums.TaskStatusEnum;
import com.qlk.vorder.domain.query.TaskQuery;
import com.qlk.vorder.service.TaskService;
import com.qlk.vorder.utils.exceptions.ExistedException;

/**
 * TaskController ：task控制器
 * 
 * @author zhaoyanan
 * @version 2016-7-28 14:14:00
 */
@Controller
@RequestMapping("task")
public class TaskController extends BaseController {

	@Autowired
	private TaskService taskService;

	/** 视图前缀 */
	private static final String viewPrefix = "task";

	private static final Log LOG = LogFactory.getLog(TaskController.class);

	/**
	 * 分页 查询数据
	 * 
	 * @param model
	 * @param page
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "")
	public String index(Model model, PageUtil page, TaskQuery query) {
		try {
			List<Task> dataList = taskService.queryTaskListWithPage(query, page);
			List<Map<String, Object>> taskStatus = TaskStatusEnum.getList();
			model.addAttribute("taskStatus", taskStatus);//任务状态枚举
			model.addAttribute("dataList", dataList);// 数据集合
			model.addAttribute("query", query);// 查询参数
			model.addAttribute("page", page);// 分页
		} catch (Exception e) {
			LOG.error("task index has error.", e);
		}
		return viewPrefix + "/index";
	}

	/**
	 * task----添加跳转
	 * 
	 * @return
	 */
	@RequestMapping(value = "addForward")
	public String addForward() {
		return viewPrefix + "/add";
	}

	/**
	 * task----添加
	 * 
	 * @param task
	 * @return
	 */
	@RequestMapping(value = "add")
	@ResponseBody
	public Wrapper<?> add(Task task) {
		try {
			// task.setCreateUser(getLoginUserCnName());
			if (taskService.insert(task)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功！");
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
			}
		} catch (ExistedException e) {
			LOG.warn("task add fail, exist task.");
			return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败，已经存在");
		} catch (Exception e) {
			LOG.error("task add has error.", e);
			return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
		}
	}

	/**
	 * task----更新跳转
	 * 
	 * @param model
	 * @param task
	 * @return
	 */
	@RequestMapping(value = "updateForward")
	public String updateForward(Model model, Task task) {
		try {
			Task taskResult = taskService.getTaskById(task.getTaskId());
			model.addAttribute("task", taskResult);
		} catch (Exception e) {
			LOG.error("task updateForward has error.", e);
		}
		return viewPrefix + "/update";
	}

	/**
	 * task----更新
	 * 
	 * @param model
	 * @param task
	 * @return
	 */
	@RequestMapping(value = "update")
	@ResponseBody
	public Wrapper<?> update(Model model, Task task) {
		try {
			// task.setUpdateUser(getLoginUserCnName());
			if(StringUtils.isNotBlank(task.getTaskIds())){
				task.setListTaskIds(java.util.Arrays.asList(task.getTaskIds()));
			}
			if (taskService.update(task)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "更新成功！");
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "更新失败！");
			}
		} catch (Exception e) {
			LOG.error("task update has error.", e);
			return WrapMapper.error();
		}
	}

	/**
	 * task----删除
	 * 
	 * @param task
	 * @return
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public Wrapper<?> delete(Task task) {
		try {
			// task.setUpdateUser(getLoginUserCnName());
			if (taskService.delete(task)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功！");
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败！");
			}
		} catch (Exception e) {
			LOG.error("task delete has error.", e);
			return WrapMapper.error();
		}
	}

	/**
	 * task----查询-无分页
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "query")
	@ResponseBody
	public Wrapper<?> query(TaskQuery query) {
		try {
			List<Task> list = taskService.queryTaskList(query);
			if (!CollectionUtils.isEmpty(list)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询配置信息失败！");
			}
		} catch (Exception e) {
			LOG.error("task query has error.", e);
			return WrapMapper.error();
		}
	}

	/**
	 * 查询task详情
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "detail", method = RequestMethod.GET)
	@ResponseBody
	public Wrapper<?> detail(TaskQuery query) {
		if (null == query || null == query.getTaskId()) {
			return illegalArgument();
		}

		try {
			Task task = taskService.getTaskById(query.getTaskId());
			if (task != null) {
				return new Wrapper<Task>().result(task);
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询task详情失败！");
			}
		} catch (Exception e) {
			LOG.warn("detail task has error.", e);
			return error();
		}
	}
}
